import { DbService } from '@app/db';
import { PricingFastCharge } from '@app/db/entities/PricingFastCharge';
import { PricingSlowCharge } from '@app/db/entities/PricingSlowCharge';
import { Injectable } from '@nestjs/common';
import { FindOptionsWhere } from 'typeorm';
import { UpdateDto } from './dto';

@Injectable()
export class PricingRuleService {
    constructor(
        private db:DbService
    ){}

    async optionsOfSlow(userId:number) {
        return this.db.pricingSlowCharge.find({ownerId : userId}, ['id', 'name'])
    }

    async optionsOfFast(userId:number) {
        return this.db.pricingFastCharge.find({ownerId : userId}, ['id', 'name'])
    }

    async fastPage(userId:number, pageNo:number, pageSize:number, name?:string) {
        let result:PaginationResult<PricingFastCharge> = {
            total : 0,
            rows : []
        }

        let where:FindOptionsWhere<PricingFastCharge> = {
            ownerId : userId
        }

        if(name) {
            where.name = name
        }

        result.total = await this.db.pricingFastCharge.count(where)

        if(result.total > 0) {
            result.rows = await this.db.pricingFastCharge.pagination(pageNo, pageSize, where, null, {id : 'DESC'})
        }
        

        return result
    }

    async slowPage(userId:number, pageNo:number, pageSize:number, name?:string) {
        let result:PaginationResult<PricingSlowCharge> = {
            total : 0,
            rows : []
        }

        let where:FindOptionsWhere<PricingSlowCharge> = {
            ownerId : userId
        }

        if(name) {
            where.name = name
        }

        result.total = await this.db.pricingSlowCharge.count(where)

        if(result.total > 0) {
            result.rows = await this.db.pricingSlowCharge.pagination(pageNo, pageSize, where, null, {id : 'DESC'})
        }
        

        return result
    }

    async addFast(name:string, rules:any[], pay:string, userId:number, time:number) {
        return this.db.pricingFastCharge.insert({
            name,
            rules,
            pay,
            ownerId : userId,
            createTime : time
        })
    }

    async addSlow(name:string, price:string, service:string, hourKwh:number, userId:number, time:number) {
        return this.db.pricingSlowCharge.insert({
            name,
            price,
            service,
            hourKwh,
            ownerId : userId,
            createTime : time
        })
    }

    async updateFast(id, dto:UpdateDto, userId:number, time:number) {
        let fields = Object.keys(dto) as (keyof PricingFastCharge)[]
        if(fields.length == 0) return

        const row = await this.db.pricingFastCharge.findOne({id : id}, ['ownerId', 'name', 'pay', 'rules'])
        if(!row || row.ownerId != userId) return false
        
        const updateData:KeyToValueType<PricingFastCharge> = {}
        if(dto.name != undefined && dto.name != row.name) {
            updateData.name = dto.name
        }
        if(dto.pay != undefined && dto.pay != row.pay) {
            updateData.pay = dto.pay
        }

        if(dto.rules != undefined) {
            updateData.rules = dto.rules
        }

        if(Object.keys(updateData).length == 0) return true

        updateData.updateTime = time
        

        return this.db.pricingFastCharge.update({id : id}, updateData)
    }

    async updateSlow(id, dto:UpdateDto, userId:number, time:number) {
        let fields = Object.keys(dto) as (keyof PricingSlowCharge)[]
        if(fields.length == 0) return

        const row = await this.db.pricingSlowCharge.findOne({id : id}, ['ownerId', 'name', 'price', 'hourKwh', 'service'])
        if(!row || row.ownerId != userId) return false
        
        const updateData:KeyToValueType<PricingSlowCharge> = {}
        if(dto.name != undefined && dto.name != row.name) {
            updateData.name = dto.name
        }


        if(dto.price != undefined && dto.price != row.price) {
            updateData.price = dto.price
        }

        if(dto.service != undefined && dto.service != row.service) {
            updateData.service = dto.service
        }

        if(dto.hourKwh != undefined && dto.hourKwh != row.hourKwh) {
            updateData.hourKwh = dto.hourKwh
        }


        if(Object.keys(updateData).length == 0) return true

        updateData.updateTime = time
        

        return this.db.pricingSlowCharge.update({id : id}, updateData)
    }

    async delFast(id:number, userId:number) {
        const row = await this.db.pricingFastCharge.findOne({id : id}, ['ownerId'])
        if(!row || row.ownerId != userId) return false
        
        return this.db.pricingFastCharge.delete({id : id})
    }

    async delSlow(id:number, userId:number) {
        const row = await this.db.pricingSlowCharge.findOne({id : id}, ['ownerId'])
        if(!row || row.ownerId != userId) return false
        
        return this.db.pricingSlowCharge.delete({id : id})
    }
}
